#!/usr/bin/env python
# test_datetools.py

__version__ = "$Revision: 1.12 $"
__author__  = 'Ramesh Balasubramanian <ramesh@finpy.org>'

import support
from support import TODO, TestCase

if __name__ == '__main__':
	support.adjust_path()
    
import datetools
import datetime
import time

class DateToolsTestCase(TestCase):
	def test_today(self):
		assert datetools.today() == datetime.date.today().toordinal()+366
		
	def test_now(self):
		assert datetools.now() == 366 + \
			datetime.date.today().toordinal() + \
			(((time.localtime()[3] * 3600) + \
			(time.localtime()[4] * 60) + \
			time.localtime()[5])/86400.0)

	def test_datenum1(self):
		assert datetools.datenum('19-may-1999') == 730259
		
	def test_datenum2(self):
		assert datetools.datenum('5/19/99') == 730259
		
	def test_datenum3(self):
		assert datetools.datenum('19-may-1999, 6:37 pm') == 730259.7757
		
	def test_datenum4(self):
		assert datetools.datenum('5/19/99/6:37 pm') == 730259.7757
		
	def test_datenum5(self):
		assert datetools.datenum('7/11/2000') == datetools.datenum('11-JUL-2000')

	def test_datenum6(self):
		assert datetools.datenum(1999,5,19) == 730259
		
	def test_datenum7(self):
		assert datetools.datenum(1999,5,19,18,37) == 730259.7757

	def test_datenum8(self):
		assert datetools.datenum(730259) == 730259
		
	def test_datenum9(self):
		assert datetools.datenum(730259.78) == 730259.78

	def test_datestr1(self):
		assert datetools.datestr(730259) == '19-May-1999'
		
	def test_datestr2(self):
		assert datetools.datestr(730123, 1) == '03-Jan-1999'
		
	def test_datestr3(self):
		assert datetools.datestr(730123, 2) == '01/03/99'
		
	def test_datestr4(self):
		assert datetools.datestr(730123.776, 0) == '03-Jan-1999 18:37:26'

	def test_datestr5(self):
		assert datetools.datestr(730123) == '03-Jan-1999'
		
	def test_datestr6(self):
		assert datetools.datestr(730123.776) == '03-Jan-1999 18:37:26'

	def test_datestr7(self):
		assert datetools.datestr(730123) == '03-Jan-1999'
		
	def test_datestr8(self):
		assert datetools.datestr(.776) == '06:37 PM' \
			or datetools.datestr(.776) == '06:37 P' # IronPython 1.0.1

	def test_datevec1(self):
		assert datetools.datevec('28-Jul-00') == (2000, 7, 28, 0, 0, 0)

	def test_datevec2(self):
		assert datetools.datevec(730695) == (2000, 7, 28, 0, 0, 0)

	def test_datevec3(self):
		assert datetools.datevec(730695.776) == (2000, 7, 28, 18, 37, 26)

	def test_datevec4(self):
		assert datetools.datevec('10-Jul-2000 19:23:12') == (2000, 7, 10, 19, 23, 12)

	def test_datevec5(self):
		assert datetools.datevec(range(730695,730698)) == \
			((2000, 7, 28, 0, 0, 0), (2000, 7, 29, 0, 0, 0), (2000, 7, 30, 0, 0, 0))

	def test_datevec6(self):
		assert datetools.datevec([730695,730696,730698]) == \
			((2000, 7, 28, 0, 0, 0), (2000, 7, 29, 0, 0, 0), (2000, 7, 31, 0, 0, 0))

	def test_datevec7(self):
		assert datetools.datevec((730695,730696,730698)) == \
			((2000, 7, 28, 0, 0, 0), (2000, 7, 29, 0, 0, 0), (2000, 7, 31, 0, 0, 0))
			
	def test_datevec8(self):
		assert (1999,5,19,0,0,0) == datetools.datevec('19-may-1999')

	def test_datevec9(self):
		assert (1999,5,19,0,0,0) == datetools.datevec('may 19, 1999')

	def test_datevec10(self):
		assert (1999,5,19,0,0,0) == datetools.datevec('19-may-99')

	def test_datevec11(self):
		assert (datetime.date.today().year,5,19,0,0,0) == datetools.datevec('19-may')

	def test_datevec12(self):
		assert (1999,5,19,0,0,0) == datetools.datevec('5/19/99')

	def test_datevec13(self):
		assert (datetime.date.today().year,5,19,0,0,0) == datetools.datevec('5/19')

	def test_datevec14(self):
		assert (1999,5,19,18,37,0) == datetools.datevec('19-may-1999, 18:37')

	def test_datevec15(self):
		assert (1999,5,19,18,37,0) == datetools.datevec('19-may-1999, 6:37 pm')

	def test_datevec16(self):
		assert (1999,5,19,18,37,0) == datetools.datevec('5/19/99/18:37')

	def test_datevec17(self):
		assert (1999,5,19,18,37,0) == datetools.datevec('5/19/99/6:37 pm')

		
	def test_year1(self):
		assert datetools.year('07-28-00 18:49:12') == 2000

	def test_year2(self):
		assert datetools.year('28-Jul-2000 18:49') == 2000

	def test_year3(self):
		assert datetools.year('7/28/00') == 2000

	def test_year4(self):
		assert datetools.year(730259) == 1999

	def test_month1(self):
		assert datetools.month('07-28-00 18:49:12') == 7

	def test_month2(self):
		assert datetools.month('28-Jul-2000 18:49') == 7

	def test_month3(self):
		assert datetools.month('7/28/00') == 7

	def test_month4(self):
		assert datetools.month(730259) == 5

	def test_day1(self):
		assert datetools.day('07-28-00 18:49:12') == 28

	def test_day2(self):
		assert datetools.day('28-Jul-2000 18:49') == 28

	def test_day3(self):
		assert datetools.day('7/28/00') == 28

	def test_day4(self):
		assert datetools.day(730259) == 19

	def test_minute1(self):
		assert datetools.minute('28-Jul-2000 18:49:12') == 49

	def test_minute2(self):
		assert datetools.minute('28-Jul-2000 18:49') == 49

	def test_minute3(self):
		assert datetools.minute('28-Jul-2000') == 0
	
	def test_hour1(self):
		assert datetools.hour('28-Jul-2000 18:49:12') == 18

	def test_hour2(self):
		assert datetools.hour('28-Jul-2000') == 0

	def test_hour3(self):
		assert datetools.hour('28-Jul-2000 6:49:12 am') == 6

	def test_hour4(self):
		assert datetools.hour('28-Jul-2000 6:49:12 pm') == 18

	def test_second1(self):
		assert datetools.second('28-Jul-2000 18:49:00') == 0

	def test_second2(self):
		assert datetools.second('28-Jul-2000 18:49:59') == 59

	def test_second3(self):
		assert datetools.second('28-Jul-2000 18:49:01') == 1

	def test_second4(self):
		assert datetools.second('28-Jul-2000') == 0
		
	def test_eomdate1(self):
		assert datetools.eomdate(2001, 2) == 730910

	def test_eomdate2(self):
		assert datetools.datestr(730910) == '28-Feb-2001'

	def test_eomdate3(self):
		assert [datetools.eomdate(year, 2) for year in (2002,2003,2004,2005)] == \
			[731275,731640,732006,732371]
	
	def test_eomday1(self):
		assert datetools.eomday(2000, 2) == 29

	def test_eomday2(self):
		assert datetools.eomday(2002, 2) == 28

	def test_eomday3(self):
		assert datetools.eomday(2001, 12) == 31

	def test_eomday4(self):
		assert datetools.eomday([2000,2001,2002,2003,2004],2) == (29,28,28,28,29)

	def test_eomday5(self):
		assert datetools.eomday(2000,[2,7,8,12]) == (29,31,31,31)

	def test_eomday6(self):
		assert datetools.eomday([2000,2001,2002,2003], [2,7,8,12]) == (29,31,31,31) 

	def test_eomday7(self):
		assert datetools.eomday((2000,2001,2002,2003,2004),2) == (29,28,28,28,29)

	def test_eomday8(self):
		assert datetools.eomday(2000,(2,7,8,12)) == (29,31,31,31)

	def test_eomday9(self):
		assert datetools.eomday((2000,2001,2002,2003), [2,7,8,12]) == (29,31,31,31) 

	def test_eomday10(self):
		assert datetools.eomday([2000,2001,2002,2003], (2,7,8,12)) == (29,31,31,31)

	def test_eomday11(self):
		assert datetools.eomday((2000,2001,2002,2003), (2,7,8,12)) == (29,31,31,31) 
				
	def test_weekday1(self):
		assert datetools.weekday(730845) == (2, 'Mon')

	def test_weekday2(self):
		assert datetools.weekday(730846) == (3, 'Tue')

	def test_weekday3(self):
		assert datetools.weekday(730847) == (4, 'Wed')

	def test_weekday4(self):
		assert datetools.weekday(730848) == (5, 'Thu')

	def test_weekday5(self):
		assert datetools.weekday(730849) == (6, 'Fri')

	def test_weekday6(self):
		assert datetools.weekday(730850) == (7, 'Sat')

	def test_weekday7(self):
		assert datetools.weekday(730851) == (1, 'Sun')

	def test_weekday8(self):		
		assert datetools.weekday('25-Dec-2000') == (2, 'Mon')

	def test_weekday9(self):
		assert datetools.weekday('26-Dec-2000') == (3, 'Tue')

	def test_weekday10(self):
		assert datetools.weekday('27-Dec-2000') == (4, 'Wed')

	def test_weekday11(self):
		assert datetools.weekday('28-Dec-2000') == (5, 'Thu')

	def test_weekday12(self):
		assert datetools.weekday('29-Dec-2000') == (6, 'Fri')

	def test_weekday13(self):
		assert datetools.weekday('30-Dec-2000') == (7, 'Sat')

	def test_weekday14(self):
		assert datetools.weekday('31-Dec-2000') == (1, 'Sun')
		
	def test_daysact1(self):
		assert datetools.daysact('9/7/2002') == 731466

	def test_daysact2(self):
		assert (731466, 731511, 731525) == \
			datetools.daysact(['09/07/2002', '10/22/2002', '11/05/2002'])

	def test_daysact3(self):
		assert (731466, 731511, 731525) == \
			datetools.daysact(('09/07/2002', '10/22/2002', '11/05/2002'))

	def test_daysact4(self):
		assert datetools.daysact('7-sep-2002',  '25-dec-2002') == 109

	def test_daysact5(self):
		assert (109,64,50) == \
			datetools.daysact(['09/07/2002', '10/22/2002', '11/05/2002'], '12/25/2002')

	def test_daysact6(self):
		assert (109,64,50) == \
			datetools.daysact(('09/07/2002', '10/22/2002', '11/05/2002'), '12/25/2002')

	def test_daysact7(self):
		assert (-109,-64,-50) == \
			datetools.daysact('12/25/2002', ['09/07/2002', '10/22/2002', '11/05/2002'])

	def test_daysact8(self):
		assert (-109,-64,-50) == \
			datetools.daysact('12/25/2002', ('09/07/2002', '10/22/2002', '11/05/2002'))
			
	def test_lweekdate1(self):
		assert datetools.datestr(datetools.lweekdate(2,2001,6)) == '25-Jun-2001'

	def test_lweekdate2(self):
		assert datetools.datestr(datetools.lweekdate(1,2007,1)) == '28-Jan-2007'

	def test_lweekdate3(self):
		assert datetools.datestr(datetools.lweekdate(2,2007,1)) == '29-Jan-2007'

	def test_lweekdate4(self):
		assert datetools.datestr(datetools.lweekdate(3,2007,1)) == '30-Jan-2007'

	def test_lweekdate5(self):
		assert datetools.datestr(datetools.lweekdate(4,2007,1)) == '31-Jan-2007'

	def test_lweekdate6(self):
		assert datetools.datestr(datetools.lweekdate(5,2007,1)) == '25-Jan-2007'

	def test_lweekdate7(self):
		assert datetools.datestr(datetools.lweekdate(6,2007,1)) == '26-Jan-2007'

	def test_lweekdate8(self):
		assert datetools.datestr(datetools.lweekdate(7,2007,1)) == '27-Jan-2007'



if __name__ == '__main__':
	import __main__
	support.run_all_tests(__main__)